package cn.xawl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.xawl.gulimall.product.entity.CategoryEntity;
import cn.xawl.gulimall.product.service.CategoryService;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.R;



/**
 * 商品三级分类
 *
 * @author lh
 * @email lhm49869999@163.com
 * @date 2020-06-17 14:15:22
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询树型结构的分类
     */
    @RequestMapping(value = "/list/tree", method = RequestMethod.GET)
    public R treeList() {
        List<CategoryEntity> tree = categoryService.queryTreeList();
        return R.ok().put("tree", tree);

    }

    /**
     * 获取分类属性分组
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改（包括其他地方关联该信息的地方也要修改）
     */
    @RequestMapping("/update")
        public R update(@RequestBody CategoryEntity category){
//		categoryService.updateById(category);
        //根据id级联更新
		categoryService.updateByIdCascade(category);

        return R.ok();
    }

    /**
     * 批量跟新顺序变化后的category
     * @param categoryEntities 要更新的category数组
     */
    @RequestMapping("/update/batch")
    public R update(@RequestBody CategoryEntity[] categoryEntities) {
        categoryService.updateBatchById(Arrays.asList(categoryEntities));
        return R.ok();
    }

    /**
     * 删除
     * 若接收的数据类型被RequestBody修饰，表示该请求需要发送POST请求
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeCategoryByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
